﻿define([
    'domReady!',
    'jquery-confirm',
    'bootstrap-table',
    'bootstrap-table-zh-CN',
    'bootstrap-select',
    'defaults-zh_CN',
    'lyear-loading',
    'validate',
    'validate_config',
    'jquery.ajaxRequest',
    'jquery.ui.widget',
    'jquery.fileupload',
    'bootstrap-datepicker.init',
], function (domReady) {
    //表单验证
    var v = $("#form").validate({
        rules: {
            account: {
                required: {
                    depends: function () {

                        return !($("#account").val().length > 0 || $("#phone").val().length > 0 || $("#email").val().length > 0)
                    }
                }
            },
            email: {
                required: {
                    depends: function () {
                        return !($("#account").val().length > 0 || $("#phone").val().length > 0 || $("#email").val().length > 0)
                    }
                },
                email: true,
            },
            phone: {
                required: {
                    depends: function () {
                        return !($("#account").val().length > 0 || $("#phone").val().length > 0 || $("#email").val().length > 0)
                    }
                },
                phone: true
            }
        },
        messages: {
            account: {
                required: "账号、手机号或邮箱至少填一个。"
            },
            email: {
                required: "账号、手机号或邮箱至少填一个。",
                email: "请填写正确的邮箱格式。",
            },
            phone: {
                required: "账号、手机号或邮箱至少填一个。",
                phone: "请填写正确的手机号格式。"
            }

        }
    });
    function init() {
        //页面数据绑定
        dataBind();
        $("#portraitUpload").fileupload({
            url: '/BMS/User/ChangePortrait',//请求发送的目标地址,
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(gif|jpe?g|png)$/i,//验证图片格式,
            maxNumberOfFiles: 1,//最大上传文件数目
            maxFileSize: 1048576000, // 文件上限1MB
            formData: { uid: uid },
            messages: {//文件错误信息
                acceptFileTypes: '文件类型不匹配',
                maxFileSize: '文件过大'
            }
        }).on("fileuploadadd", function (e, data) {

        }).on("fileuploadprogressall", function (e, data) {
            //显示上传进度条
            $('#progress').show();
            var progress = parseInt(data.loaded / data.total * 100, 10);

            $('#progress .progress-bar').css(
                'width', progress + '%'
            );
        }).on("fileuploadfail", function (e, data) {
            //上传请求失败时触发的回调函数

            $.alert({
                title: "上传失败",
                icon: 'mdi mdi-alert',
                type: 'orange',
                content: data.errorThrown,
            });
        }).on("fileuploaddone", function (e, data) {
            //上传请求成功时触发的回调函数
            dataBind()
            $('#progress').hide();
            $('#progress .progress-bar').css(
                'width', '0%'
            );

        })
    }

    function dataBind() {
        $('.page-loading').ajaxRequest("/BMS/User/GetUser", { uid: uid }, function (data) {
            $(".img-avatar").attr("src", data.data.portrait)
            $("#portrait").val(data.data.portrait);
            setValue("#account", data.data.account)
            setValue("#phone", data.data.phone)
            setValue("#email", data.data.email)
            setValue("#nickname", data.data.nickname)
            setValue("#realname", data.data.realname)
            setValue("#gender", data.data.gender)
            setValue("#birthday", data.data.birthday)
            setValue("#registerTime", data.data.registerTime)
        })
    }
    function bind() {
        $(document).on('click', '.edit', function () {
            var $edit = $(this)
            var $submit = $edit.parent().find(".submit")
            var $input = $edit.parent().find("input,select")
            var $value = $edit.parent().find(".value");
            $input.val($value.html())
            $edit.addClass('hide');
            $value.addClass('hide')
            $submit.removeClass('hide')
            $input.removeClass('hide')

        })
        $(document).on('click', '.event-submit', function () {
            if (!v.form()) {
                return;
            }
            var account = $("#account").val();
            var phone = $("#phone").val();
            var email = $("#email").val()
            var nickname = $("#nickname").val()
            var realname = $("#realname").val()
            var genderText = $("#gender").val()
            var gender = 0;
            switch (genderText) {
                case "未知": gender = 0; break;
                case "男": gender = 1; break;
                case "女": gender = 2; break;
            }
            var birthday = $("#birthday").val()
            $close = $(this).parent().find(".event-close");

            $(this).ajaxRequest("/BMS/User/Save", {
                uid: uid,
                account: account,
                phone: phone,
                email: email,
                nickname: nickname,
                realname: realname,
                gender: gender,
                birthday: birthday
            }, function (data) {
                $.msgSuccess(data.message);
                dataBind();
                $close.trigger("click")
            });

        })
        $(document).on('click', '.event-close', function () {
            var $close = $(this)
            var $submit = $close.parent()
            var $input = $submit.parent().find("input,select")
            var $value = $submit.parent().find(".value");
            var $edit = $submit.parent().find(".edit")
            $edit.removeClass('hide');
            $value.removeClass('hide')
            $submit.addClass('hide')
            $input.addClass('hide')

            if ($close.parent().parent().find("label").length > 1) {
                $close.parent().parent().find("label")[1].remove()
            }
        })
        $(document).on('click', '#btnPortrait', function () {
            $("#portraitUpload").trigger('click')
        })
    }

    function setValue(id, value) {
        $(id).parent().find(".value").html(value);
        $(id).val(value);
    }

    $(document).ready(function () {
        init();
        bind();
    })
})